package com.ithiema.ui;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class RegisterJFrame extends JFrame implements MouseListener {
    //跟注册相关的代码，都写在这个界面中

    JTextField username = new JTextField();
    JPasswordField password = new JPasswordField();
    JPasswordField passwordAgain = new JPasswordField();
    JButton Register = new JButton();
    JButton Reset = new JButton();

    public RegisterJFrame()
    {
        initJFrame();

        initView();

        //让显示显示出来，建议写在最后
        this.setVisible(true);
    }

    private void initView() {
        //1.添加用户名文字
        JLabel usernameText = new JLabel(new ImageIcon("image/register/注册用户名.png"));
        usernameText.setBounds(110, 138, 79, 17);
        this.getContentPane().add(usernameText);

        //2.添加用户名输入框

        username.setBounds(195, 135, 200, 30);
        this.getContentPane().add(username);

        //3.添加密码文字
        JLabel passwordText = new JLabel(new ImageIcon("image/register/注册密码.png"));
        passwordText.setBounds(125, 198, 64, 16);
        this.getContentPane().add(passwordText);

        //4.密码输入框
        password.setBounds(195, 195, 200, 30);
        this.getContentPane().add(password);

        //5.再次输入密码文字
        JLabel passwordAgainText = new JLabel(new ImageIcon("image/register/再次输入密码.png"));
        passwordAgainText.setBounds(95,257,96,17);
        this.getContentPane().add(passwordAgainText);

        //6.再次输入密码输入框
        passwordAgain.setBounds(195,254,200,30);
        this.getContentPane().add(passwordAgain);

        //添加注册按钮
        Register.setBounds(123, 310, 128, 47);
        Register.setIcon(new ImageIcon("image/register/注册按钮.png"));
        //去除按钮边框
        Register.setBorderPainted(false);
        //去除按钮的背景
        Register.setContentAreaFilled(false);
        //给登录按钮绑定鼠标事件
        Register.addMouseListener(this);
        //将按钮添加到页面中
        this.getContentPane().add(Register);

        //添加重置按钮
        Reset.setBounds(256, 310, 120, 47);
        Reset.setIcon(new ImageIcon("image/register/重置按钮.png"));
        //去除按钮边框
        Reset.setBorderPainted(false);
        //去除按钮背景
        Reset.setContentAreaFilled(false);
        //给注册按钮绑定鼠标事件
        Reset.addMouseListener(this);
        //添加到页面
        this.getContentPane().add(Reset);

        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("image/login/background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
    }

    private void initJFrame() {
        //设置窗口大小
        this.setSize(470,418);
        //设置界面的标题
        this.setTitle("拼图 注册");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        //同时出现的只有一个界面
        //WindowConstants.EXIT_ON_CLOSE=3 为常量，也可以用3传入参数
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        this.setLayout(null);
    }

    public void showJDialog(String content)
    {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200,150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建JLabel对象管理文字并添加到弹框当中
        JLabel Warning = new JLabel();
        Warning.setText(content);
        Warning.setBounds(100,65,200,150);
        jDialog.getContentPane().add(Warning);

        //显示弹框
        jDialog.setVisible(true);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        if(e.getSource() == Register)
        {
            //点击了注册按钮
            //先要两次密码是否一致
            if(password.getText().equals(passwordAgain.getText()))
            {
                //还要比较文件中有没有出现过该用户名
                try {
                    if(HasUsername(username.getText())&&
                            username.getText()!=null&&
                            username.getText().length()>=3)
                    {
                        System.out.println("该用户名已存在");
                        showJDialog("该用户名已存在,请重新输入");
                        username.setText("");
                        password.setText("");
                        passwordAgain.setText("");
                        return;
                    }
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }

                if(username.getText() == null ||
                        username.getText().length()==0)
                {
                    showJDialog("用户名不为空！");
                    return;
                }

                if(!IsRightUsername(username.getText()))
                {
                    System.out.println("该用户名不合法");
                    showJDialog("用户名由数字字母组成,最少4位");
                    username.setText("");
                    password.setText("");
                    passwordAgain.setText("");
                    return;
                }

                if(password.getText() == null||password.getText().length() == 0)
                {
                    showJDialog("密码不为空！");
                    return;
                }

                if(!password.getText().matches("\\S*(?=\\S{6,})(?=\\S*\\d)" +
                        "(?=\\S*[a-z])\\S*"))
                {
                    showJDialog("密码包含小写字母和数字，最少6位");
                    return;
                }

                System.out.println("注册成功");
                //显示弹窗
                showJDialog("注册成功！回到登录界面");
                //将信息传入文件中
                String UserInfo = username.getText();
                String PasswordInfo = password.getText();
                try {
                    writeInfo("username="+UserInfo+"&password="+PasswordInfo+"&count=0");
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                this.setVisible(false);
                new LoginJFrame();
            }
            else
            {
                //两次密码不一致
                System.out.println("两次密码不一致，请重新输入密码");
                //显示弹窗
                showJDialog("两次密码不一致，请重新输入密码");
                //操作
                password.setText("");
                passwordAgain.setText("");
            }
        }

        if(e.getSource() == Reset)
        {
            username.setText("");
            password.setText("");
            passwordAgain.setText("");
        }
    }

    public boolean IsRightUsername(String username)
    {
        if(username == null||username.length()==0)
        {
            return false;
        }
        //anyRule生成
        return username.matches("[a-zA-Z0-9]{4,16}");
    }
    public boolean HasUsername(String username) throws IOException
    {
        BufferedReader br = new BufferedReader(new FileReader("userinfo.txt"));

        ArrayList<String> list = new ArrayList<>();
        String line;
        while ((line=br.readLine())!=null)
        {
            if(!line.equals(""))
            {
                list.add(line);
            }
        }

        br.close();

        if(list.size() == 0)
        {
            return false;
        }

        for (String s : list) {
            String user = s.split("&")[0].split("=")[1];
            if(user.equals(username))
            {
                return true;
            }
        }
        return false;
    }

    public void writeInfo(String content) throws IOException
    {
        BufferedWriter bw = new BufferedWriter(new FileWriter("userInfo.txt",true));

        bw.write(content);

        bw.newLine();

        bw.close();
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
